{# 加载静态资源模块 #}
{% load static %}
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>年级与学生信息 - Layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="{% static 'css/layui.css' %}">
</head>
<body class="layui-layout-body">

<div class="layui-container" style="padding: 20px;">
  <!-- 左右布局容器 -->
  <div class="layui-row layui-col-space20">

    <!-- 左侧：年级班级信息 -->
    <div class="layui-col-md4">
      <h3>年级班级列表</h3>
      <table class="layui-hide" id="gradeTable" lay-filter="gradeTable"></table>
      <div id="grade-page" style="text-align:center;margin-top:15px;"></div>
    </div>

    <!-- 右侧：学生信息 -->
    <div class="layui-col-md8">
      <h3>学生信息列表</h3>
      <table class="layui-hide" id="studentTable" lay-filter="studentTable"></table>
      <div id="student-page" style="text-align:center;margin-top:15px;"></div>
    </div>

  </div>
</div>

<!-- 引入 Layui JS -->
<script src="{% static 'js/layui.js' %}"></script>

<!-- 页面逻辑脚本 -->
<script>
layui.use(['table', 'laypage'], function(){
  var table = layui.table;
  var laypage = layui.laypage;

  // 渲染年级班级表格
  function renderGradeTable(page) {
    table.render({
      elem: '#gradeTable',
      url: '/api/grades/?page=' + page,
      page: false,
      cols: [[
        {field: 'id', title: 'ID', width: 80, sort: true},
        {field: 'grade_name', title: '年级班级', minWidth: 150},
        {fixed: 'right', title: '操作', width: 100, templet: function(){
          return '<a class="layui-btn layui-btn-xs">编辑</a>';
        }}
      ]]
    });
  }

  // 渲染学生信息表格
  function renderStudentTable(page) {
    table.render({
      elem: '#studentTable',
      url: '/api/students/?page=' + page,
      page: false,
      cols: [[
        {field: 'id', title: 'ID', width: 80, sort: true},
        {field: 'name', title: '姓名', width: 120},
        {field: 'age', title: '年龄', width: 80},
        {field: 'gender', title: '性别', width: 100},
        {field: 'phone', title: '电话', width: 150},
        {field: 'student_id', title: '学号', width: 150},
        {field: 'grade', title: '所属班级', minWidth: 150},
        {fixed: 'right', title: '操作', width: 100, templet: function(){
          return '<a class="layui-btn layui-btn-xs">编辑</a>';
        }}
      ]]
    });
  }

  // 初始化分页组件
  function initPagination() {
    // 年级分页
    laypage.render({
      elem: 'grade-page',
      count: {{ grades.paginator.count }},
      limit: 10,
      curr: parseInt('{{ grades.number }}') || 1,
      theme: '#FF5722',
      layout: ['count', 'prev', 'page', 'next'],
      jump: function(obj, first) {
        if (!first) {
          renderGradeTable(obj.curr);
          history.replaceState(null, '', '?grade_page=' + obj.curr);
        }
      }
    });

    // 学生分页
    laypage.render({
      elem: 'student-page',
      count: {{ students.paginator.count }},
      limit: 10,
      curr: parseInt('{{ students.number }}') || 1,
      theme: '#FF5722',
      layout: ['count', 'prev', 'page', 'next'],
      jump: function(obj, first) {
        if (!first) {
          renderStudentTable(obj.curr);
          history.replaceState(null, '', '?student_page=' + obj.curr);
        }
      }
    });
  }

  // 首次加载数据
  renderGradeTable(1);
  renderStudentTable(1);
  initPagination();
});
</script>

</body>
</html>
